package com.lantu.wisdom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lantu.framework.common.utils.BeanMapper;
import com.lantu.wisdom.common.enums.DeletedEnum;
import com.lantu.wisdom.common.utils.CreateCodeUtil;
import com.lantu.wisdom.dto.AddAppraisalTypeDTO;
import com.lantu.wisdom.dto.SearchAppraisalTypeDTO;
import com.lantu.wisdom.dto.vo.SearchAppraisalTypeVO;
import com.lantu.wisdom.model.WAppraisalType;
import com.lantu.wisdom.mapper.WAppraisalTypeMapper;
import com.lantu.wisdom.service.WAppraisalTypeApi;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 鉴定类型表 服务实现类
 * </p>
 *
 * @author zch
 * @since 2021-06-15
 */
@Service
public class WAppraisalTypeServiceImpl extends ServiceImpl<WAppraisalTypeMapper, WAppraisalType> implements WAppraisalTypeApi {

    @Override
    public boolean addAppraisalType(AddAppraisalTypeDTO dto) {
        WAppraisalType w = BeanMapper.map(dto, WAppraisalType.class);
        w.setTypeCode(CreateCodeUtil.createBookTypeCode());
        w.setCreateTime(System.currentTimeMillis());
        w.setDeleted(DeletedEnum.FalseDeleted.getState());
        boolean save = this.save(w);
        return save;
    }

    /**
     * 获取首页数据
     */
    @Override
    public List<SearchAppraisalTypeDTO> getAppraisalTypeList() {
        List<SearchAppraisalTypeDTO> listDto = new ArrayList<>();
        //获取level 1
        List<WAppraisalType> list = this.list(new LambdaQueryWrapper<WAppraisalType>().eq(WAppraisalType::getLevel, 1));
        List<SearchAppraisalTypeDTO> typeDTOS = BeanMapper.mapList(list, SearchAppraisalTypeDTO.class);
        //获取level 2
        for (SearchAppraisalTypeDTO type : typeDTOS) {
            List<WAppraisalType> list1 = list(new LambdaQueryWrapper<WAppraisalType>().eq(WAppraisalType::getParentCode, type.getTypeCode()));
            List<SearchAppraisalTypeDTO> typeDTOS1 = BeanMapper.mapList(list1, SearchAppraisalTypeDTO.class);
            for (SearchAppraisalTypeDTO type1:typeDTOS1) {
                List<WAppraisalType> list2 = list(new LambdaQueryWrapper<WAppraisalType>().eq(WAppraisalType::getParentCode, type1.getTypeCode()));
                if(!list2.isEmpty()){
                    type1.setChildren(BeanMapper.mapList(list2,SearchAppraisalTypeDTO.class));
                }
            }
            type.setChildren(typeDTOS1);
        }
        listDto.addAll(typeDTOS);
        return listDto;
    }

    /**
     * 根据code查询分类
     *
     * @param code
     */
    @Override
    public List<SearchAppraisalTypeVO> getAppraisalTypeByCode(String code) {
        List<WAppraisalType> list = this.list(new LambdaQueryWrapper<WAppraisalType>().eq(WAppraisalType::getParentCode, code).isNull(WAppraisalType::getIntroduction));
        if(list.size()>0){
            List<SearchAppraisalTypeVO> searchAppraisalTypeVOS = BeanMapper.mapList(list, SearchAppraisalTypeVO.class);
            return  searchAppraisalTypeVOS;
        }
        return null;
    }

    /**
     * 首页推荐
     *
     * @return
     */
    @Override
    public List<SearchAppraisalTypeVO> MainPageIntroduction() {

        List<WAppraisalType> list = this.list(new LambdaQueryWrapper<WAppraisalType>().eq(WAppraisalType::getIntroduction, 1));
        if(list.size()>0){
            List<SearchAppraisalTypeVO> searchAppraisalTypeVOS = BeanMapper.mapList(list, SearchAppraisalTypeVO.class);
            return  searchAppraisalTypeVOS;
        }
        return null;
    }
}
